﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;
using CoolERP_DAL;

namespace CoolERP_BLL
{
    public class XSHTMXBBLL : BaseBLL<XSHTMXB>
    {       
        public XSHTMXBBLL() { }

        public List<XSHTMXBModel> GetXSHTMXB(Func<XSHTMXBModel, bool> exp, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {

            XSHTMXBDAL _dal = new XSHTMXBDAL(context);
            return _dal.GetXSHTMXB(exp, orderExpr, pageIndex, pageSize, out  totalCount);
        }

        /// <summary>
        /// 待处理合同（不在生产计划列表中的合同明细）
        /// </summary>
        public List<XSHTMXBModel> GetSCJH_HTMXB(Dictionary<string, string> dc, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var predicate = PredicateExtensions.True<XSHTMXBModel>();
            if (dc != null && dc.Count > 0)
            {
                if (dc.ContainsKey("HTID") && !string.IsNullOrEmpty(dc["HTID"]))
                {
                    Guid keyVal = new Guid(dc["HTID"]);
                    predicate = predicate.AndEx(t => t.XSHTID == keyVal);
                }
                if (dc.ContainsKey("HTZT") && !string.IsNullOrEmpty(dc["HTZT"]))
                {
                    int keyVal = Convert.ToInt32(dc["HTZT"]);
                    predicate = predicate.AndEx(t => t.HTZT == keyVal);
                }
                if (dc.ContainsKey("NotID") && !string.IsNullOrEmpty(dc["NotID"]))
                {
                    string[] ids = dc["NotID"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    predicate = predicate.AndEx(t => !ids.Contains(t.ID.ToString()));
                }
                if (dc.ContainsKey("HTBH") && !string.IsNullOrEmpty(dc["HTBH"]))
                    predicate = predicate.AndEx(t => t.HTBH.Contains(dc["HTBH"]));
                if (dc.ContainsKey("KHJC") && !string.IsNullOrEmpty(dc["KHJC"]))
                    predicate = predicate.AndEx(t => t.KHJC.Contains(dc["KHJC"]));
                if (dc.ContainsKey("CPBH") && !string.IsNullOrEmpty(dc["CPBH"]))
                    predicate = predicate.AndEx(t => t.CPBH.Contains(dc["CPBH"]));
                if (dc.Keys.Contains("sDate") && !string.IsNullOrEmpty(dc["sDate"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["sDate"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    predicate = predicate.AndEx(t => t.CHSJ >= keyVal);
                }
                if (dc.Keys.Contains("eDate") && !string.IsNullOrEmpty(dc["eDate"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["eDate"]).AddDays(1).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    predicate = predicate.AndEx(t => t.CHSJ <= keyVal);
                }
            }
            XSHTMXBDAL _dal = new XSHTMXBDAL(context);
            return _dal.GetSCJH_HTMXB(predicate.Compile(), orderExpr, pageIndex, pageSize, out  totalCount);
        }
    }
}
